package com.shawn.dubbo.api;

import com.shawn.model.mongo.RecommendUser;
import com.shawn.model.mongo.UserLike;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询今日缘分值最高佳人
     *
     * @param toUserId 用户id
     * @return {@link TodayBest}
     */
    @Override
    public RecommendUser getTodayLove(Long toUserId) {
        //根据userId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query, RecommendUser.class);

    }

    @Override
    public void saveRecommendUser(RecommendUser todayLove) {

    }

    //分页查询
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建Query对象
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        //总记录数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //配置分页参数
        query.skip((long) (page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        //查询数据列表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page, pagesize, Math.toIntExact(count), recommendUsers);
    }

    @Override
    public RecommendUser findByBothId(Long userId, Long toUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId))
                .addCriteria(Criteria.where("toUserId").is(toUserId));
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public List<RecommendUser> getRecommendCards(Long userId, int number) {
        //找出当前用户已经喜欢或者不喜欢的用户id
        List<UserLike> userLikes = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        Query query = new Query();
        if (likeIds.size()>0){
            query.addCriteria(Criteria.where("userId").not().in(likeIds));
        }
        query.addCriteria(Criteria.where("toUserId").is(userId))
                .limit(10);
        return mongoTemplate.find(query,RecommendUser.class);
    }

    @Override
    public List<RecommendUser> findAll(Long toUserId) {
        //构建查询条件对象
        Query query = new Query();
        //根据用户id进行查询
        query.addCriteria(Criteria.where("toUserId").is(toUserId));
        //调用mongoTemplate进行查询
        return mongoTemplate.find(query, RecommendUser.class);
    }

    @Override
    public List<RecommendUser> findByCondition(Long toUserId) {
        //构建查询条件对象
        Query query = new Query();
        //根据用户id进行查询
        query.addCriteria(Criteria.where("toUserId").is(toUserId))
                //根据缘分值进行降序排序
                .with(Sort.by(Sort.Order.desc("score")))
                //查询缘分值大于80的推荐佳人
                .addCriteria(Criteria.where("score").gte(80))
                //仅返回推荐佳人id
                .fields().include("userId").include("score");
        //调用mongoTemplate进行查询
        return mongoTemplate.find(query, RecommendUser.class);
    }
}
